import user from '@/api/user'
import goods from "@/api/goods";
import mall from "@/api/mall";
import order from "@/api/order";

// 获取秒杀数据
export const loadSeckillsInfo = ({ commit }) => {
  return new Promise((resolve, reject) => {
    goods.list({}).then(response => {
      console.log('查询秒杀商品成功');
      var goodsData = response.data;
      resolve(goodsData);
    }).catch(reason => {
      console.log('查询秒杀商品失败!');
    });
  }).then(data=>{
    const date = new Date();
    const hours = date.getHours();
    const minute = date.getMinutes();
    const seconds = date.getSeconds();
    console.log([hours, minute, seconds]);
    // 距离开始秒杀时间
    const deadline = {
      hours: 1,
      minute: 38,
      seconds: 36
    };
    commit('SET_SECKILLS_INFO', [data, deadline]);
  });
};

// 获取轮播(营销)图片
export const loadCarouselItems = ({ commit }) => {
  return new Promise((resolve, reject) => {
    const data = {
      carouselItems: [
        'static/img/nav/1.jpg',
        'static/img/nav/2.jpg',
        'static/img/nav/3.jpg',
        'static/img/nav/4.jpg',
        'static/img/nav/5.jpg'
      ],
      activity: [
        'static/img/nav/nav_showimg1.jpg',
        'static/img/nav/nav_showimg2.jpg'
      ]
    };
    commit('SET_CAROUSELITEMS_INFO', data);
  });
};

// 加载电脑专栏数据
export const loadComputer = ({ commit }) => {
  return new Promise((resolve, reject) => {
    const computer = {
      title: '电脑数码',
      link: [ '电脑馆', '游戏极品', '装机大师', '职场焕新', '女神频道', '虚拟现实', '二合一平板', '电子教育', '万物周刊' ],
      detail: [
        {
          bigImg: 'static/img/index/computer/item-computer-1.jpg',
          itemFour: [
            {
              title: '电脑馆',
              intro: '笔记本999元限量秒！',
              img: 'static/img/index/computer/item-computer-2.jpg'
            },
            {
              title: '外设装备',
              intro: '1000减618',
              img: 'static/img/index/computer/item-computer-1-3.jpg'
            },
            {
              title: '电脑配件',
              intro: '联合满减最高省618',
              img: 'static/img/index/computer/item-computer-1-4.jpg'
            },
            {
              title: '办公生活',
              intro: '5折神券 精品文具',
              img: 'static/img/index/computer/item-computer-1-5.jpg'
            }
          ],
          itemContent: [
            'static/img/index/computer/item-computer-1-6.jpg',
            'static/img/index/computer/item-computer-1-7.jpg',
            'static/img/index/computer/item-computer-1-8.jpg'
          ]
        },
        {
          bigImg: 'static/img/index/computer/item-computer-2-1.jpg',
          itemFour: [
            {
              title: '平板电脑',
              intro: '爆款平板12期免息',
              img: 'static/img/index/computer/item-computer-2-2.jpg'
            },
            {
              title: '智能酷玩',
              intro: '抢999减666神券',
              img: 'static/img/index/computer/item-computer-2-3.jpg'
            },
            {
              title: '娱乐影音',
              intro: '大牌耳机低至5折',
              img: 'static/img/index/computer/item-computer-2-4.jpg'
            },
            {
              title: '摄影摄像',
              intro: '大牌相机5折抢',
              img: 'static/img/index/computer/item-computer-2-5.jpg'
            }
          ],
          itemContent: [
            'static/img/index/computer/item-computer-2-6.jpg',
            'static/img/index/computer/item-computer-2-7.jpg',
            'static/img/index/computer/item-computer-2-8.jpg'
          ]
        }
      ]
    };
    commit('SET_COMPUTER_INFO', computer);
  });
};

// 加载爱吃专栏数据
export const loadEat = ({ commit }) => {
  return new Promise((resolve, reject) => {
    const eat = {
      title: '爱吃',
      link: [ '休闲零食', '坚果', '牛奶', '饮料冲调', '食用油', '大米', '白酒', '红酒', '烧烤食材', '牛排', '樱桃' ],
      detail: [
        {
          bigImg: 'static/img/index/eat/item-eat-1-1.jpg',
          itemFour: [
            {
              title: '粮油调味',
              intro: '买2免1',
              img: 'static/img/index/eat/item-eat-1-2.jpg'
            },
            {
              title: '饮料冲调',
              intro: '第二件半价',
              img: 'static/img/index/eat/item-eat-1-3.jpg'
            },
            {
              title: '休闲零食',
              intro: '满99减40',
              img: 'static/img/index/eat/item-eat-1-4.jpg'
            },
            {
              title: '中外名酒',
              intro: '满199减100',
              img: 'static/img/index/eat/item-eat-1-5.jpg'
            }
          ],
          itemContent: [
            'static/img/index/eat/item-eat-1-6.jpg',
            'static/img/index/eat/item-eat-1-7.jpg',
            'static/img/index/eat/item-eat-1-8.jpg'
          ]
        },
        {
          bigImg: 'static/img/index/eat/item-eat-2-1.jpg',
          itemFour: [
            {
              title: '东家菜',
              intro: '丰富好味',
              img: 'static/img/index/eat/item-eat-2-2.jpg'
            },
            {
              title: '东家菜',
              intro: '丰富好味',
              img: 'static/img/index/eat/item-eat-2-2.jpg'
            },
            {
              title: '东家菜',
              intro: '丰富好味',
              img: 'static/img/index/eat/item-eat-2-2.jpg'
            },
            {
              title: '东家菜',
              intro: '丰富好味',
              img: 'static/img/index/eat/item-eat-2-2.jpg'
            }
          ],
          itemContent: [
            'static/img/index/eat/item-eat-2-6.jpg',
            'static/img/index/eat/item-eat-2-7.jpg',
            'static/img/index/eat/item-eat-2-8.jpg'
          ]
        }
      ]
    };
    commit('SET_EAT_INFO', eat);
  });
};

// 请求获得商品详细信息
export const loadGoodsInfo = ({ commit }, id) => {
  commit('SET_LOAD_STATUS', true);
  return new Promise((resolve, reject) => {
    // setTimeout(() => {
    //   const data = {
    //     goodsImg: [
    //       'static/img/goodsDetail/item-detail-1.jpg',
    //       'static/img/goodsDetail/item-detail-2.jpg',
    //       'static/img/goodsDetail/item-detail-3.jpg',
    //       'static/img/goodsDetail/item-detail-4.jpg'
    //     ],
    //     title: '苹果8/7手机壳iPhone7 Plus保护壳全包防摔磨砂硬外壳',
    //     tags: ['满69-20元', '关注产品★送钢化膜', 'BIT配次日达'],
    //     discount: ['满148减10', '满218减20', '满288减30'],
    //     promotion: ['跨店满减', '多买优惠'],
    //     remarksNum: 6000,
    //     setMeal: [
    //       [
    //         {
    //           img: 'static/img/goodsDetail/pack/1.jpg',
    //           intro: '4.7英寸-深邃蓝',
    //           price: 28.0
    //         },
    //         {
    //           img: 'static/img/goodsDetail/pack/2.jpg',
    //           intro: '4.7英寸-星空黑',
    //           price: 29.0
    //         },
    //         {
    //           img: 'static/img/goodsDetail/pack/3.jpg',
    //           intro: '5.5英寸-香槟金',
    //           price: 28.5
    //         }
    //       ],
    //       [
    //         {
    //           img: 'static/img/goodsDetail/pack/4.jpg',
    //           intro: '5.5英寸-玫瑰金',
    //           price: 32.0
    //         },
    //         {
    //           img: 'static/img/goodsDetail/pack/5.jpg',
    //           intro: '5.5英寸-深邃蓝',
    //           price: 32.0
    //         },
    //         {
    //           img: 'static/img/goodsDetail/pack/6.jpg',
    //           intro: '5.5英寸-星空黑',
    //           price: 35.0
    //         }
    //       ],
    //       [
    //         {
    //           img: 'static/img/goodsDetail/pack/7.jpg',
    //           intro: '4.7英寸-香槟金',
    //           price: 26.0
    //         },
    //         {
    //           img: 'static/img/goodsDetail/pack/8.jpg',
    //           intro: '4.7英寸-玫瑰金',
    //           price: 25.0
    //         },
    //         {
    //           img: 'static/img/goodsDetail/pack/9.jpg',
    //           intro: '4.7英寸-中国红',
    //           price: 28.0
    //         }
    //       ]
    //     ],
    //     hot: [
    //       {
    //         img: 'static/img/goodsDetail/hot/1.jpg',
    //         price: 28.0,
    //         sale: 165076
    //       },
    //       {
    //         img: 'static/img/goodsDetail/hot/2.jpg',
    //         price: 36.0,
    //         sale: 135078
    //       },
    //       {
    //         img: 'static/img/goodsDetail/hot/3.jpg',
    //         price: 38.0,
    //         sale: 105073
    //       },
    //       {
    //         img: 'static/img/goodsDetail/hot/4.jpg',
    //         price: 39.0,
    //         sale: 95079
    //       },
    //       {
    //         img: 'static/img/goodsDetail/hot/5.jpg',
    //         price: 25.0,
    //         sale: 5077
    //       },
    //       {
    //         img: 'static/img/goodsDetail/hot/6.jpg',
    //         price: 20.0,
    //         sale: 3077
    //       }
    //     ],
    //     goodsDetail: [
    //       'static/img/goodsDetail/intro/1.jpg',
    //       'static/img/goodsDetail/intro/2.jpg',
    //       'static/img/goodsDetail/intro/3.jpg',
    //       'static/img/goodsDetail/intro/4.jpg'
    //     ],
    //     param: [
    //       {
    //         title: '商品名称',
    //         content: 'iPhone 7手机壳'
    //       },
    //       {
    //         title: '商品编号',
    //         content: '10435663237'
    //       },
    //       {
    //         title: '店铺',
    //         content: '1Mall'
    //       },
    //       {
    //         title: '商品毛重',
    //         content: '100.00g'
    //       },
    //       {
    //         title: '商品产地',
    //         content: '中国大陆'
    //       },
    //       {
    //         title: '机型',
    //         content: 'iPhone 7'
    //       },
    //       {
    //         title: '材质',
    //         content: 'PC/塑料'
    //       },
    //       {
    //         title: '款式',
    //         content: '软壳'
    //       },
    //       {
    //         title: '适用人群',
    //         content: '通用'
    //       }
    //     ],
    //     remarks: {
    //       goodAnalyse: 90,
    //       remarksTags: [ '颜色可人', '实惠优选', '严丝合缝', '极致轻薄', '质量没话说', '比定做还合适', '完美品质', '正品行货', '包装有档次', '不容易发热', '已经买第二个', '是全覆盖' ],
    //       remarksNumDetail: [ 2000, 3000, 900, 1 ],
    //       detail: [
    //         {
    //           username: 'p****1',
    //           values: 3,
    //           content: '颜色很好看，质量也不错！，还送了个指环，想不到哦！',
    //           goods: '4.7英寸-深邃蓝',
    //           create_at: '2018-05-15 09:20'
    //         },
    //         {
    //           username: '13****1',
    //           values: 5,
    //           content: '手感没的说，是硬壳，后背带有磨砂手感。很不错，很喜欢，还加送了钢化膜，支架环，物超所值，准备再买一个。很满意。物流很快。很愉快的一次购物！',
    //           goods: '5.5英寸-玫瑰金',
    //           create_at: '2018-05-13 15:23'
    //         },
    //         {
    //           username: '3****z',
    //           values: 4.5,
    //           content: '相当轻薄，店家还送了一大堆配件，*元非常值得！',
    //           goods: '4.7英寸-玫瑰金',
    //           create_at: '2018-05-05 12:25'
    //         },
    //         {
    //           username: 'gd****c',
    //           values: 3.5,
    //           content: '就是我想要的手机壳，壳子很薄，手感不错，就像没装手机壳一样，想要裸机手感的赶快下手了。',
    //           goods: '4.7英寸-中国红',
    //           create_at: '2018-04-06 16:23'
    //         },
    //         {
    //           username: 'r****b',
    //           values: 4.5,
    //           content: '壳子还不错，送的膜也可以，不过还是感觉膜小了那么一点，屏幕没法完全覆盖。对了，壳子稍微有点硬，可能会伤漆，所以不要频繁取壳就好。',
    //           goods: '4.7英寸-中国红',
    //           create_at: '2018-03-15 19:24'
    //         },
    //         {
    //           username: 'd****e',
    //           values: 5,
    //           content: '磨砂的，相当漂亮，尺寸非常合适！精工细作！',
    //           goods: '5.5英寸-星空黑',
    //           create_at: '2018-03-10 10:13'
    //         }
    //       ]
    //     }
    //   };
    //   commit('SET_GOODS_INFO', data);
    //   commit('SET_LOAD_STATUS', false);
    // }, 300);

    goods.detail(id).then(resp => {

      commit('SET_GOODS_INFO', resp.data);
      commit('SET_LOAD_STATUS', false);
      resolve(resp.data);
    }).catch(err => {
      commit('SET_LOAD_STATUS', false);
      reject();
    })
  });
};

// 获取商品列表
export const loadGoodsList = ({ commit }, param) => {
  commit('SET_LOAD_STATUS', true);
  return new Promise((resolve, reject) => {
    goods.search(param).then(resp => {
      resolve(resp.data);
    }).catch(err => {
      console.error(err);
      reject(err);
    });
  }).then(data => {
    const goodsData = {
      asItems: data.recommendGoodsInfos,
      goodsList: data.goodsInfos
    };
    commit('SET_GOODS_LIST', goodsData);
    commit('SET_LOAD_STATUS', false);
  }).catch(err => {
    commit('SET_LOAD_STATUS', false);
  });
};

// 添加购物车
export const addShoppingCart = ({ commit }, data) => {
  return new Promise((resolve, reject) => {

    var itemData = {
      userId: data.userId,
      goodsId: data.goodsId,
      count: data.count,
      img: data.package.img,
      meal: data.package.intro,
      price: data.package.price,
      title: data.title
    };

    mall.addCartItem(itemData).then(resp => {
      commit('ADD_SHOPPING_CART', data);
      resolve();
    })
  });
};

// 获取用户推荐
export const loadRecommend = ({ commit }) => {
  return new Promise((resolve, reject) => {
    const data = [
      [
        {
          img: 'static/img/otherBuy/1.jpg',
          intro: 'iPhone7/6s/8钢化膜苹果7Plus全屏全覆盖3D抗蓝',
          price: 29.00
        },
        {
          img: 'static/img/otherBuy/2.jpg',
          intro: '苹果数据线 苹果iPhoneX/6s/7plus/8充电线',
          price: 36.00
        },
        {
          img: 'static/img/otherBuy/3.jpg',
          intro: '苹果8/7/6/6s钢化膜 iphone8/7/6s/6钢化玻璃',
          price: 19.00
        },
        {
          img: 'static/img/otherBuy/4.jpg',
          intro: 'iPhone6s/7钢化膜苹果8 Plus手机膜抗蓝光非全屏',
          price: 28.00
        }
      ],
      [
        {
          img: 'static/img/otherBuy/5.jpg',
          intro: '苹果6s手机壳iPhone6s Plus保护壳防摔全',
          price: 28.00
        },
        {
          img: 'static/img/otherBuy/6.jpg',
          intro: 'iPhone7/8手机壳手机套防摔磨砂保护壳星空黑☆全包保护',
          price: 30.00
        },
        {
          img: 'static/img/otherBuy/7.jpg',
          intro: '数据线 适用于苹果iPhone 6s/6plus/7plus/8/X',
          price: 18.00
        },
        {
          img: 'static/img/otherBuy/8.jpg',
          intro: 'iPhone8/7/6S/6钢化膜 苹果8/7/6s/6玻璃膜 手机高',
          price: 15.00
        }
      ]
    ];
    commit('SET_RECOMMEND_INFO', data);
  });
};

export const loadAddress = ({ commit }, data) => {
  return new Promise((resolve, reject) => {

    const addresses = [
    ];

    user.getReceiveAddress(data).then(resp => {
      resp.data.content.forEach(e => {
        addresses.push({
            addressId: e.id,
            name: e.name,
            province: e.province,
            city: e.city,
            region: e.region,
            address: e.detailAddress,
            phone: e.phoneNumber,
            postalcode: e.postCode
          });
      });

      commit('SET_USER_ADDRESS', addresses);
    });

  });
};

export const loadShoppingCart = ({ commit }, userId) => {
  return new Promise((resolve, reject) => {

    mall.getCart({
      "userId": userId
    }).then(resp => {

      var items = [];
      resp.data.items.forEach(i => {
        items.push({
            goodsId: i.goodsId,
            count: i.amount,
            img: i.img,
            package: i.meal,
            price: i.price,
            title: i.goodsName
          });
      });

      commit('SET_SHOPPING_CART', items);
      resolve();
    });
  });
};

// 添加注册用户
export const addSignUpUser = ({ commit }, data) => {
  return new Promise((resolve, reject) => {
    const userArr = localStorage.getItem('users');
    let users = [];
    if (userArr) {
      users = JSON.parse(userArr);
    }
    users.push(data);
    localStorage.setItem('users', JSON.stringify(users));
  });
};

// 用户登录
export const login = ({ commit }, data) => {

  return new Promise((resolve, reject) => {

    user.login(data).then(response => {
      console.log('登录成功')

      var data = response.data;

      if(data.status){
        var userInfo = {
          userId: data.userId,
          username: data.userName,
          mail: '',
          phone: data.mobile,
          token: data.token
        };
        localStorage.setItem('loginInfo', JSON.stringify(userInfo));
        commit('SET_USER_LOGIN_INFO', userInfo);
        resolve(true);
      } else {
        resolve(false);
      }
    }).catch(reason => {
      console.log('登录成功')
      resolve(false);
    });

  });
};

// 退出登陆
export const signOut = ({ commit }) => {
  localStorage.removeItem('loginInfo');
  commit('SET_USER_LOGIN_INFO', {});
};

// 判断是否登陆
export const isLogin = ({ commit }) => {
  const user = localStorage.getItem('loginInfo');
  if (user) {
    commit('SET_USER_LOGIN_INFO', JSON.parse(user));
  }
};

export const createOrderFromCart = ({ commit }, data) => {
  return new Promise((resolve, reject) => {
    order.createOrderFromCart(data).then(resp => {

      commit('SET_SHOPPING_CART', {});  //清空购物车
      resolve();
    });
  });
};
